# -*- coding: utf-8 -*-

#   This file is part of deckbuilder.
#
#    DeckBuilder is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    DeckBuilder is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with deckbuilder; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import network

PACKET_CONNECT = 0
PACKET_CHAT = 1
PACKET_DECK = 2
PACKET_DRAW = 3
PACKET_SHUFFLE = 4
PACKET_CARDMOVE = 5
PACKET_CARDFLIP = 6
PACKET_LIFECHANGE = 7
PACKET_PHASE = 8
PACKET_ROLL = 9

class Packet():
    def __init__(self,id):
        self._id = id
        self._buffer = ''
        self._fields = 0

    def Write(self, data):
        self._buffer += unicode(data) + '/f/'
        self._fields += 1

    def Build(self):
        raw = unicode(self._id) + '/f/' + unicode(self._fields) + '/f/' + self._buffer
        packet = raw.encode('utf-8')
        return packet

class ConnectPacket(Packet):
    def __init__(self, nickname):
        Packet.__init__(self, PACKET_CONNECT) # ID del pacchetto
        self.Write(nickname)

class ChatPacket(Packet):
    def __init__(self, message):
        Packet.__init__(self, PACKET_CHAT) # ID del pacchetto
        self.Write(message)

class DeckPacket(Packet):
    def __init__(self, deck):
        Packet.__init__(self, PACKET_DECK) # ID del pacchetto
        for c in deck:
            self.Write(c.Code)
            self.Write(c.IsSide)

class DrawPacket(Packet):
    def __init__(self, reveal):
        Packet.__init__(self, PACKET_DRAW) # ID del pacchetto
        self.Write(reveal)

class ShufflePacket(Packet):
    def __init__(self, deck):
        Packet.__init__(self, PACKET_SHUFFLE) # ID del pacchetto
        for c in deck:
            self.Write(c.GetSerial())

class CardMovePacket(Packet):
    def __init__(self, serial, dest, dest2=0, x=0, y=0):
        Packet.__init__(self, PACKET_CARDMOVE) # ID del pacchetto
        self.Write(serial)
        self.Write(dest)
        self.Write(dest2)
        self.Write(x)
        self.Write(y)

class CardFlipPacket(Packet):
    def __init__(self, serial, state):
        Packet.__init__(self, PACKET_CARDFLIP) # ID del pacchetto
        self.Write(serial)
        self.Write(state)

class LifeChangePacket(Packet):
    def __init__(self, change):
        Packet.__init__(self, PACKET_LIFECHANGE) # ID del pacchetto
        self.Write(change)

class PhasePacket(Packet):
    def __init__(self, phase):
        Packet.__init__(self, PACKET_PHASE) # ID del pacchetto
        self.Write(phase)

class RollPacket(Packet):
    def __init__(self, faces, n):
        Packet.__init__(self, PACKET_ROLL) # ID del pacchetto
        self.Write(faces)
        self.Write(n)